#!/bin/bash
j_script_dir=`dirname $0`
j_jenny_root_dir=`(cd $j_script_dir/..; pwd)`

. $j_jenny_root_dir/common/function

usage() {
	echo "jenny install [-h] [git url]"
	echo "    install jenny."
	echo "    -h                print this help."
	echo "    [git url]         install jenny, use given url, or use the remote url of this git project."
	exit 1
}

if [ "$1" = "-h" ]; then 
	usage
fi

die_install() {
	rm -rf ~/.jenny
	die
}

[[ ! -e ~/.jenny ]] || die 'Jenny was already installed. Use "jenny update" for updating.' 

j_jenny_git_url=$1

if [ "$1." = "." ]; then
	extract_git_url j_jenny_git_url
fi

echo $j_jenny_git_url

[[ ! -z $j_jenny_git_url ]] || die 'Failed to extract jenny git url. Please try "jenny install git_url"'

mkdir -p ~/.jenny/jenny || die_install 

git clone $j_jenny_git_url ~/.jenny/jenny || die_install


~/.jenny/jenny/jenny update	|| die_install 'Failed to update during installation'

